/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package step;

import step.Node;

/**
 * A socket node is a place where a local STEP graph interfaces with the
 * network in order to interface with remote STEPs.
 */
public abstract class SocketNode extends Node
{
    public SocketNode(String id)
    {
	super(id);
    }

	public SocketNode() {
		super();
	}

	protected String GetInfoXML(String prot, 
				    String role, 
				    java.net.URI peer) {
		return super.GetInfoXML(
			"<protocol>" + prot + "</protocol>" + 
			"<role>" + role + "</role>" +
			"<peer>" + peer.toString() + "</peer>");
	}

	public static SocketNode MakeSocket(String id,
					    String protocol,
					    String role,
					    String PeerID,
					    java.net.URI PeerURI)
	  throws step.parse.SyntaxError {
		if ((!role.equals("sender")) &&
		    (!role.equals("receiver")))
			throw new step.parse.SyntaxError("Role must be \"sender\" or \"receiver\"");
		if (protocol.equals("GET")) {
			if (role.equals("sender"))
				return new step.SenderGetSocketNode(id, PeerID, PeerURI);
			else
				return new step.ReceiverGetSocketNode(id, PeerID, PeerURI);
		} else if (protocol.equals("POST")) {
			if (role.equals("sender"))
				return new step.SenderPostSocketNode(id, PeerID, PeerURI);
			else
				return new step.ReceiverPostSocketNode(id, PeerID, PeerURI);
		} else if (protocol.equals("UDP")) {
			if (role.equals("sender"))
				return new step.SenderUdpSocketNode(id, PeerID, PeerURI);
			else
				return new step.ReceiverUdpSocketNode(id, PeerID, PeerURI);
		} else
			throw new step.parse.SyntaxError("Unknown socketing protocol");
	}

    protected abstract void Evaluate() throws EvaluationFailure;
    
    public String TagName() { return "socket"; }
}

